﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using WebService.VO;

namespace WebService.DAO
{
    public class DeviceDAO
    {
        private dbConnection conn;
        /// <summary>
        /// khởi tạo đối tượng xử lý dl
        /// </summary>
        public DeviceDAO()
        {
            conn = new dbConnection();
        }
        /// <summary>
        /// lấy danh sách danh mục
        /// </summary>
        /// <returns></returns>

        public DataTable getItemDeviceList()
        {
            return conn.GetDataTable("Select * From tbl_Device");
        }
        /// <summary>
        /// Get ID of Device in tbl_Device
        /// </summary>
        /// <param name="deviceID"></param>
        /// <returns></returns>
        public int getID(string deviceID)
        {
            string query = "SELECT ID FROM tbl_Device WHERE DeviceID = @DeviceID";

            IDictionary<string, string> d = new Dictionary<string, string>();
            d.Add("@DeviceID", deviceID);

            return conn.ExecuteQuery(query, d, false);
        }
        /// <summary>
        /// Check Device exist
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool check(string name)
        {
            string sqlSelect = "Select count(*) From tbl_Device Where DeviceID = @DeviceID";
            IDictionary<string, string> d = new Dictionary<string, string>();

            d.Add("@DeviceID", name);

            int kq = conn.ExecuteScalar(sqlSelect, d, false);
            if (kq > 0)
                return true;
            return false;
        }


        /// <summary>
        /// Thêm mới 1 danh mục
        /// </summary>
        public void insert(DeviceVO device)
        {
            string sqlInsert = "INSERT INTO tbl_Device VALUES(@DeviceId, @UserName, @Email, @RegisterDate)";

            IDictionary<string, string> d = new Dictionary<string, string>();

            d.Add("@DeviceId", device.DeviceID);
            d.Add("@UserName", device.UserName);
            d.Add("@Email", device.Email);
            d.Add("@RegisterDate", device.RegisterDate.ToString("hh:mm:ss MM/dd/yyyy"));

            conn.ExecuteQuery(sqlInsert, d, false);

        }

        /// <summary>
        /// Xóa thông tin một danh mục
        /// </summary>
        public void delete(string id)
        {
            string sql = "DELETE FROM tbl_Device WHERE Id = @id";

            IDictionary<string, string> d = new Dictionary<string, string>();

            d.Add("@id", id);
            conn.ExecuteQuery(sql, d, false);
        }
        /// <summary>
        /// Cập nhật thông tin một danh mục
        /// </summary>
        public void update(DeviceVO device)
        {
            string sqlInsert = "UPDATE tbl_Device SET Email=@Email WHERE DeviceID=@DeviceId";

            IDictionary<string, string> d = new Dictionary<string, string>();

            d.Add("@DeviceId", device.DeviceID);
            d.Add("@Email", device.Email);
            

            conn.ExecuteQuery(sqlInsert, d, false);
        }

        /// <summary>
        /// Them dia diem hien tai cua nguoi dung vao
        /// </summary>
        /// <param name="ItemDevice"></param>
        public void insertHistory(DeviceHistoryVO deviceHistory)
        {
            string sqlInsert = "INSERT INTO tbl_HistoryLocation VALUES(@DeviceID, @Date,@Lat,@Lng)";

            IDictionary<string, string> d = new Dictionary<string, string>();

            d.Add("@DeviceID", deviceHistory.Device.Id.ToString());
            d.Add("@Date", deviceHistory.Date.ToString("hh:mm:ss MM/dd/yyyy"));
            d.Add("@Lat", deviceHistory.X.ToString());
            d.Add("@Lng", deviceHistory.Y.ToString());
            conn.ExecuteQuery(sqlInsert, d, false);
        }

        /// <summary>
        /// kiem tra Device da ton tai hay chua
        /// </summary>
        /// <returns></returns>
        public DeviceVO getDevice(string id)
        {
            DeviceVO item = null;
            DataTable dt = conn.GetDataTable("Select * From tbl_Device WHERE DeviceID='" + id + "'");
            if (dt != null)
            {
                if (dt.Rows[0] != null)
                {
                    item = new DeviceVO();
                    item.Id = Convert.ToInt16(dt.Rows[0]["ID"]);
                    if (dt.Rows[0]["DeviceID"].ToString() != null)
                    {
                        item.DeviceID = dt.Rows[0]["DeviceID"].ToString();
                    }
                    else
                    {
                        item.DeviceID = "NULL";
                    }
                    if (dt.Rows[0]["UserName"] != null)
                    {
                        item.UserName = dt.Rows[0]["UserName"].ToString(); ;
                    }
                    if (dt.Rows[0]["Email"] != null)
                    {
                        item.Email = dt.Rows[0]["Email"].ToString();
                    }
                    if (dt.Rows[0]["RegisterDate"] != null)
                    {
                        item.RegisterDate = (DateTime)dt.Rows[0]["RegisterDate"];
                    }
                }
            }
            return item;
        }
     
    }
}